/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.feature.solr.local.core.group;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.response.Group;
import org.apache.solr.client.solrj.response.GroupCommand;
import org.apache.solr.client.solrj.response.QueryResponse;
import yhao.infra.feature.solr.local.SearchCriteria;
import yhao.infra.feature.solr.local.core.SolrCallbackHandler;
import yhao.infra.feature.solr.local.core.SolrTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Group查询回调处理类
 *
 * @author yoara
 */
public class GroupCallbackHandler<T> extends SolrCallbackHandler<T> {
    private Logger logger = Logger.getLogger(this.getClass());
    private GroupSolrPagination pagination;

    public GroupCallbackHandler(SolrTemplate<T> solrTemplate, SearchCriteria criteria, GroupSolrPagination<T> pagination) {
        super(solrTemplate, criteria, pagination);
        this.pagination = pagination;
    }

    @Override
    public List<T> parseQueryResponse(QueryResponse response) {
        Map<String, SolrGroupBean> groupFieldsMap = criteria.getGroupQueryInfo().getGroupFieldsMap();
        pagination.setGroupFields(criteria.getGroupQueryInfo().getGroupFields());

        if (response.getGroupResponse() != null) {
            List<GroupCommand> values = response.getGroupResponse().getValues();
            pagination.setRecordCount(values.size());
            pagination.setItemMap(new HashMap<>());
            for (GroupCommand groupCommand : values) {
                String groupName = groupCommand.getName();
                SolrGroupBean groupBean = groupFieldsMap.get(groupName);
                groupBean.setDocsCount(groupCommand.getMatches());
                groupBean.setNgCount(groupCommand.getNGroups());
                Map<String,SolrGroupResultBean<T>> groupResult = new HashMap<>();
                groupBean.setGroupResult(groupResult);
                List<T> itemAll = new ArrayList();
                for (Group group : groupCommand.getValues()) {
                    try {
                        SolrGroupResultBean resultBean = new SolrGroupResultBean();
                        resultBean.setGroupValue(group.getGroupValue());
                        resultBean.setGroupResult(solrTemplate.solrDocumentsToEntities(group.getResult()));
                        resultBean.setNumFound(group.getResult().getNumFound());
                        resultBean.setStart(group.getResult().getStart());
                        groupResult.put(group.getGroupValue(),resultBean);
                        itemAll.addAll(resultBean.getGroupResult());
                    } catch (Exception e) {
                        logger.error(e);
                    }
                }
                pagination.getItemMap().put(groupName,itemAll);
            }
        }
        //group分页数据需要使用单独的方法
        return null;
    }
}